﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
/// <summary>
/// 进行技能解析 创建回收管理
/// .技能系统来管理所有技能
/// </summary>
public class SkillSystem:MonoSingle<SkillSystem>
{
    private Dictionary<int, SkillInstance> m_SkillInstance = new Dictionary<int, SkillInstance>();
    public SkillSystem() { }

    #region 
    /// <summary>
    /// 不同等级技能 可能相关数据不一样
    /// </summary>
    /// <param name="skillId"></param>
    /// <returns></returns>
    public int CalcSkillBaseId(int skillId)
    {
        return 0;
    }
    /// <summary>
    /// 主要用于 技能等级的相关
    /// </summary>
    /// <param name="skillBaseId"></param>
    /// <param name="skillLevel"></param>
    /// <returns></returns>
    public int CalcSkillId(int skillBaseId,int skillLevel)
    {
        return 0;
    }

    public int CalcSkillLevel(int skillId)
    {
        return 0;
    }

    public int GetTriggerCount(int skillBaseId,string typeName)
    {
        return 0;
    }

    public SkillInstance NewSkillInstance(int skillbaseId)
    {
        SkillInstance skillInstance;
        if(!m_SkillInstance.TryGetValue(skillbaseId,out skillInstance))
        {

        }
        return skillInstance;
    }

    public void RecycleSkillInstance(SkillInstance skill)
    {

    }
    public void Reset()
    {

    }
    #endregion

    #region 解析技能脚本
    private bool ParseScript(string filename)
    {
        bool ret = false;
        //try
        //{
        //    //TextAsset txt = ResMgr.SG.Load(filename, typeof(UnityEngine.Object)) as TextAsset;
        //    MemoryStream ms = new MemoryStream(txt.bytes);
        //    StreamReader sr = new StreamReader(ms, Encoding.Default, true);
        //    if (sr != null)
        //        ret = LoadScriptFromStream(sr);
        //}
        //catch (Exception e)
        //{
        //    string err = "Exception:" + e.Message + "\n" + e.StackTrace + "\n";
        //}
        return ret;
    }
    private bool LoadScriptFromStream(StreamReader sr)
    {
        bool bracket = false;
        SkillInstance skill = null;
        do
        {
            string line = sr.ReadLine();
            if (line == null)
                break;
            line = line.Trim();

            if (line.StartsWith("//") || line == "") continue;//空或换行

            if (line.StartsWith("skill"))
            {
                int start = line.IndexOf("(");
                int end = line.IndexOf(")");
                if (start == -1 || end == -1)
                    Debug.LogError("ParseScript Error, start == -1 || end == -1" + line);

                int length = end - start - 1;
                if (length <= 0)
                {
                    Debug.LogError("ParseScript Error, length <= 1 " + line);
                    return false;
                }

                string args = line.Substring(start + 1, length);
                int skillId = (int)Convert.ChangeType(args, typeof(int));
                skill = new SkillInstance();
                AddSkillInstanceToPool(skillId, skill, true);
            }
            else if (line.StartsWith("{"))
            {
                bracket = true;
            }
            else if (line.StartsWith("}"))
            {
                bracket = false; //括号结束
                //按时间排序
                skill.m_SkillTriggers.Sort((left, right) =>
                {
                    if (left.GetStartTime() > right.GetStartTime())
                    {
                        return -1;
                    }
                    else if (left.GetStartTime() == right.GetStartTime())
                    {
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                });
            }
            else
            {
                //解析trigger
                if (skill != null && bracket == true)
                {
                    int start = line.IndexOf("(");
                    int end = line.IndexOf(")");
                    if (start == -1 || end == -1)
                        Debug.LogError("ParseScript Error, " + line);

                    int length = end - start - 1;
                    if (length <= 0)
                    {
                        Debug.LogError("ParseScript Error, length <= 1, " + line);
                        return false;
                    }

                    string type = line.Substring(0, start); //类型名
                    string args = line.Substring(start + 1, length); //参数
                    args = args.Replace(" ", "");
                    ISkillTrigger trigger = SkillTriggerMgr.Instance.CreateTrigger(type, args);
                    if (trigger != null)
                    {
                        skill.m_SkillTriggers.Add(trigger);
                    }
                }
            }
        }
        while (true);
        return true;
    }
    //将技能添加进 技能池
    private void AddSkillInstanceToPool(int skillId, SkillInstance skillInstance, bool bo)
    {
        if (m_SkillInstance.ContainsKey(skillId))
            return;
        m_SkillInstance.Add(skillId, skillInstance);
    }
    #endregion
}
